package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//http://localhost:8080/Web205_206/calculate?num_1=199&num_2=999
public class CalculateServlet extends HttpServlet {
    //映射post请求
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    //get请求
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取提交的数据：请求参数   url属于request对象中的一部分
        //根据请求参数的name属性获取value值
        String num_1 = request.getParameter("num_1");
        String num_2 = request.getParameter("num_2");
        System.out.println("num_1 = " + num_1);
        System.out.println("num_2 = " + num_2);
        int sum = Integer.parseInt(num_1) + Integer.parseInt(num_2);
        //利用响应将计算结果返回给用户
        PrintWriter writer = response.getWriter();
        writer.write(num_1 + "+" + num_2 + "=" + sum);
        writer.flush();
        writer.close();
    }
}
